Page({

  /**
   * 页面的初始数据
   */
  data: {
    picUrl: "",
    des: "",
    orderId: 0,
    receiveUserId: 0,
    imageSrc: ['../../icons/jia.png'],
  },
  post: function (e) {
    var that = this
    wx.chooseImage({
      count:1,
      sizeType: ['original', 'compressed'], // original 原图，compressed 压缩图，默认二者都有
      sourceType: ['album', 'camera'],
      success: function (res) {
        console.log(res.tempFilePaths);
        var img = res.tempFilePaths;
        that.setData({
          picUrl: img[0]
        })
      },
    })
  },
  cancel: function () {
    this.setData({
      picUrl: ""
    })
  },
  // 上传图片，修改图片
  // upload: function () {
  //   let that = this;
  //   wx.chooseImage({
  //     sizeType: ['original', 'compressed'], // original 原图，compressed 压缩图，默认二者都有
  //     sourceType: ['album', 'camera'], // album 从相册选图，camera 使用相机，默认二者都有
  //     success: function (res) {
  //       let url = res.tempFilePaths,
  //         num = res.tempFilePaths.length;
  //       for (let i = 0; i < num; i++) {
  //         wx.uploadFile({
  //           url: 'https://www.xiaozheng8.com/upload',
  //           filePath: url[i],
  //           name: 'file',
  //           header: {
  //             'Content-Type': 'multipart/form-data'
  //           },
  //           success: function (res) {
  //             console.log(url[i])
  //             that.data.imageSrc[i] = res.data;
  //             that.setData({
  //               imageSrc: that.data.imageSrc
  //             })
  //           }
  //         })
  //       }
  //       console.log(that.data.imageSrc)

  //     },
  //     fail: function (res) {

  //     }
  //   })
  // },

  // /**
  // * 删除图片事件函数
  // */
  // cancel: function (e) {
  //   let arr = this.data.imageSrc;
  //   let a = e.currentTarget.dataset.index
  //   var that = this;
  //   if (this.data.imageSrc.length > 1) {
  //     that.data.imageSrc.splice(a, 1);
  //     that.setData({
  //       imageSrc: that.data.imageSrc
  //     })
  //   } else {
  //     that.data.imageSrc[a] = "../../icons/jia.png",
  //       that.setData({
  //         imageSrc: that.data.imageSrc
  //       })
  //   }


  // },

  // 提交投诉内容
  complain: function () {
    var that = this;
 
    if (this.data.picUrl==""){
      wx.showToast({
        title: '请上传图片',
        icon:"none"
      })
    } else if (that.data.des == null || that.data.des == "") {
      wx.showToast({
        title: '请填写投诉内容',
        icon: "none"
      })
    } else {
      wx.uploadFile({
        url: 'https://www.xiaozheng8.com/upload',
        // filePath: that.data.imageSrc,
        filePath: that.data.picUrl,
        name: "file",
        header: {
          "Content-Type": "multipart/form-data"
        },
        success: function (res) {
          console.log(res.data);
          // var img = JSON.parse(res.data);
          var img =res.data;
          wx.showModal({
            title: '提示',
            content: '是否确认提交投诉',
            confirmText: "确定",
            cancelText: "取消",
            success: function (res) {
              if (res.confirm) {
                if (that.data.receiveUserId == "" || that.data.receiveUserId == null || that.data.receiveUserId == undefined) {
                  wx.request({
                    url: 'https://www.xiaozheng8.com/serviceComplain/add',
                    method: "post",
                    data: {
                      orderId: that.data.orderId,
                      content: that.data.des,
                      img: res.data.data
                    },
                    success: function (res) {
                      console.log(res);
                      if (res.code == 200) {
                        wx.navigateBack({})
                        wx.showToast({
                          title: '提交成功',
                          icon: 'succes',
                          duration: 1000,
                          mask: true
                        })
                        
                      }else{
                        wx.showModal({
                          title: '提示',
                          content: '信息提交失败，请重新上传',
                          showCancel: false,
                          success: function (res) {
                          }
                        })
                      }
                    },
                    fail: function (res) {
                      console.log(res);
                    }
                  })
                } else if (that.data.orderId == "" || that.data.that.data.orderId == null || that.data.that.data.orderId == undefined) {
                  wx.request({
                    url: 'https://www.xiaozheng8.com/serviceComplain/add',
                    method: "post",
                    data: {
                      receiveUserId: that.data.receiveUserId,
                      content: that.data.des,
                      img: img
                    },
                    success: function (res) {
                      console.log(res);
                      if (res.data.code == 200) {
                        wx.navigateBack({})
                        wx.showToast({
                          title: '提交成功',
                          icon: 'success',
                          duration: 1000,
                          mask: true
                        })

                      } else {
                        wx.showModal({
                          title: '提示',
                          content: '信息提交失败，请重新上传',
                          showCancel: false,
                          success: function (res) {
                            wx.navigateBack({})
                          }
                        })
                      }
                    },
                    fail: function (res) {
                      console.log(res);
                    }
                  })

                }

                

              }
            }
          })
        }
      })




    }

  },
  des: function (e) {
    this.setData({
      des: e.detail.value
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options);
    var that = this;
    if (options.userId == undefined && options.receiveUserId == undefined) {

    } else if (options.userId == undefined) {
      that.setData({
        receiveUserId: options.receiveUserId
      })
    } else {
      thats.setData({
        userId: options.userId
      })
    }




  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})